import tkinter as tk
class MyCar:
    width=100
    height=220
    def __init__(self,canvas, person,x,y):
        self.canvas = canvas
        self.person = person
        self.blood = 1
        self.score = 0
        self.x = x
        self.y = y
        self.speed = 0
        self.photo = None
        
        self.load_image()
    def move(self, direction):
        if direction == "left":
            self.x -= 5
        elif direction == "right":
            self.x += 5
        elif direction == "up":
            self.y -= 5
        elif direction == "down":
            self.y += 5
        # 更新图片位置
        self.canvas.coords(self.image, self.x, self.y)
    def cursh(self):
        self.image = "./images/mycar-1.png"
    def load_image(self):
        if self.person == "p1":
            self.image_path = "./images/mycar-1.png"
        else:
            self.image_path = "./images/mycar-2.png"
        self.photo = tk.PhotoImage(self.image_path)
        if self.image is None:
            self.image = self.canvas.create_image(self.x, self.y, anchor=tk.NW, image=self.photo)
        else:
            self.canvas.itemconfig(self.image, image=self.photo)        